﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace PlaxoSyncWM {
    public partial class SyncForm : Form {
        public IdDeltas idDeltas;
        public Settings settings;
        public MobileContactsFile mcf;

        public SyncForm() {
            InitializeComponent();
            PlaxoLogin pl = new PlaxoLogin();
            settings = new Settings();
            mcf = new MobileContactsFile();
            statusBar.Text = "Trying Plaxo...";
            if (pl.checkAccount()) {
                statusBar.Text = "Logged in";
                settings.SaveSetting("loginSuccessful", "y");
            }
            else {}
        }

        //TODO: Ensure that you write the new MobileContacts file after the sync operation on adds, edits or deletes
        //TODO: after slow sync, check to see if there are adds on the server side - then add the server side entries into client, then do a Map command sending the server the respective client id's

        private void syncButton_Click(object sender, EventArgs e) {
            PlaxoSync ps = new PlaxoSync();
            if (settings.GetSetting("slowSync").Equals("n")) {
                string url = ps.createSlowAddUrl();
                if (ps.slow(url)) {
                    statusBar.Text = "First Sync successful!";
                    incrementAnchors();
                    settings.SaveSetting("slowSync", "y");
                }
                else statusBar.Text = "First Sync unsuccessful!";
            }

            SyncContacts(ps, ps.createAddsOnDeviceURL(), labelAddsOnDevice, "Adds", addsOnDeviceTick);
            SyncContacts(ps, ps.createEditsOnDeviceUrl(), labelEditsOnDevice, "Edits", editsOnDeviceTick);
            SyncContacts(ps, ps.createDeletesOnDeviceURL(), labelDeletesOnDevice, "Deletes", deletesOnDeviceTick);
            File.Delete("\\Program Files\\PlaxoSyncWM\\MobileContacts.app");
            mcf.create();    
        }
    
        private void SyncContacts(PlaxoSync ps, string url, Label label, string actionPerformed, Control pictureBoxId) {
            if (url != "") {
                label.BackColor = Color.OrangeRed;
                ps.slow(url);
                incrementAnchors();
                pictureBoxId.Visible = true;
                label.BackColor = Color.FromArgb(154, 205, 50);
                label.Text = actionPerformed + " on Device: Synced!";
            }
            else {
                label.BackColor = Color.FromArgb(154, 205, 50);
                label.Text = actionPerformed + " on Device: Synced!";
            }
            Refresh();
        }

        public void incrementAnchors() {
            int lastAnchor = int.Parse(settings.GetSetting("lastAnchor")) + 1;
            int nextAnchor = int.Parse(settings.GetSetting("nextAnchor")) + 1;
            settings.SaveSetting("lastAnchor", lastAnchor.ToString());
            settings.SaveSetting("nextAnchor", nextAnchor.ToString());
        }

        private void SyncForm_Load(object sender, EventArgs e) {}

        private void menuItemOptions_Click(object sender, EventArgs e)
        {
            Form mainForm = new MainForm();
            mainForm.Show();
        }
    }
}